package com.szgr.baoyin.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.szgr.baoyin.MainActivity;
import com.szgr.baoyin.MyApplication;
import com.szgr.baoyin.R;
import com.szgr.baoyin.pay.BankSuccessActivity;
import com.szgr.baoyin.utils.Constant;
import com.szgr.baoyin.utils.MyTool;
import com.szgr.baoyin.utils.RLog;
import com.szgr.baoyin.utils.ToastUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class BankTranslateActivity extends BaseActivity {

    private String pay_sn;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 22:
                    Map<String,String> map = (Map<String, String>) msg.obj;
                    tv_sn.setText(map.get("pay_sn"));
                    tv_company.setText(map.get("company_name"));
                    tv_bankName.setText(map.get("bank_name"));
                    tv_account.setText(map.get("bank_account"));
                    tv_money.setText(map.get("pay_amount"));
                    break;
                case 77:
                    ToastUtils.showToast("提交成功");
                    Intent intent = new Intent(BankTranslateActivity.this, BankSuccessActivity.class);
                    startActivity(intent);
                    finish();
                    break;
            }
        }
    };
    private TextView tv_sn;
    private TextView tv_company;
    private TextView tv_bankName;
    private TextView tv_account;
    private TextView tv_money;
    private EditText tv_num;
    private HashMap<String, String> hashMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_bank_translate);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initActionBar() {
        actionBarTitle.setText("银行转账");
    }

    @Override
    public void initView() {
        Intent intent = getIntent();

        findViewById(R.id.btn_sure).setOnClickListener(this);

        tv_sn = (TextView) findViewById(R.id.order_sn);
        tv_company = (TextView) findViewById(R.id.order_company);
        tv_bankName = (TextView) findViewById(R.id.order_bank_name);
        tv_account = (TextView) findViewById(R.id.order_bank_account);
        tv_money = (TextView) findViewById(R.id.order_money);
        tv_num = (EditText) findViewById(R.id.order_num);

        if(intent != null && intent.hasExtra("pay_sn")){
            pay_sn = intent.getStringExtra("pay_sn");
            getBank(pay_sn);
        }
    }

    private void getBank(final String sn) {
        StringRequest sr = new StringRequest(Request.Method.POST, Constant.URL_BANK_SEND,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        RLog.s("----bank--->" + s);
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            JSONObject datas = jsonObject.getJSONObject("datas");
                            JSONObject bankzz = datas.getJSONObject("bankzz");
                            hashMap = new HashMap<>();
                            hashMap.put("company_name", bankzz.getString("company_name"));
                            hashMap.put("bank_name", bankzz.getString("bank_name"));
                            hashMap.put("bank_account", bankzz.getString("bank_account"));

                            JSONObject pay_info = datas.getJSONObject("pay_info");
                            hashMap.put("pay_sn1", pay_info.getString("pay_sn"));
                            hashMap.put("pay_amount", pay_info.getString("pay_amount"));

                            JSONArray order_list = datas.getJSONArray("order_list");
                            JSONObject object = (JSONObject) order_list.get(0);
                            hashMap.put("pay_sn", object.getString("order_sn"));

                            mHandler.obtainMessage(22, hashMap).sendToTarget();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map = new HashMap<>();
                map.put("key",MyApplication.getmInstance().getKey());
                map.put("pay_sn",sn);
                RLog.s("---params--->" + map.toString());
                return map;
            }
        };
        MyApplication.getmInstance().addToRequestQueue(sr);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.action_bar_left:
                finish();
                break;
            case R.id.btn_sure:
                if(!MyTool.isEmpty(tv_num)) {
                    surePay(hashMap.get("pay_sn1"),tv_num.getText().toString().trim());
                }
                break;
        }
    }

    private void surePay(final String sn, final String trade_no) {
        StringRequest sr = new StringRequest(Request.Method.POST, Constant.URL_CONFIRM,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        RLog.s("----bank--->" + s);
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            String datas = jsonObject.getString("datas");
                            if("操作成功".equals(datas)){
                                mHandler.obtainMessage(77).sendToTarget();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map = new HashMap<>();
                map.put("key",MyApplication.getmInstance().getKey());
                map.put("pay_sn",sn);
                map.put("trade_no",trade_no);
                return map;
            }
        };
        MyApplication.getmInstance().addToRequestQueue(sr);
    }
}
